﻿using UnityEngine;
/// <summary>
/// GameObjects with this script Attached to it will automatically create a Enemy Entity
/// </summary>
class EnemyView : MonoBehaviour, IBaseView,IEnemyPositionListener,IEnemySpriteFlipListener
{
   public EnemyEntity enemyEntity;
    SpriteRenderer SpriteRender;
    /// <summary>
    /// creates entity with name gameobject refference and HP
    /// </summary>
    /// 
    private void Awake()
    {
        SpriteRender = GetComponent<SpriteRenderer>();
        EntitySetup();
        BattleRadiusSetup();
    }
    private void EntitySetup()
    {
        enemyEntity = Contexts.sharedInstance.enemy.CreateEntity();
        int num = Contexts.sharedInstance.enemy.count;
        enemyEntity.AddName(name+ " " + num);
       name = enemyEntity.name.value;
        enemyEntity.AddDamage(SetDamage(enemyEntity.name.value));
        enemyEntity.AddHitPoint(100);
        enemyEntity.AddSpeed(.04f);
        enemyEntity.isRoaming = true;
        enemyEntity.AddRoamingTimer(1.5f);
        enemyEntity.AddRoamingAngle(0);
       enemyEntity.AddDirection(Direction.Right);
        enemyEntity.AddPosition(transform.position);
        enemyEntity.AddSpawnPosition(transform.position);
        enemyEntity.AddSpriteFlip(false, false);
       enemyEntity.AddEnemySpriteFlipListener(this);
        enemyEntity.AddEnemyPositionListener(this);
        enemyEntity.AddBaseView(this);
        




    }
    /// <summary>
    /// Creates a gameobject with only a circle collider for an agro radius. If the player enters this collider the enemy
    /// will begin to attack the player
    /// </summary>
    private void BattleRadiusSetup()
    {
        GameObject BattleRadius = new GameObject
        {
            name = "BattleRadius",
            tag = "EnemyZone",
        };
        BattleRadius.transform.position = transform.position;
        BattleRadius.transform.parent = transform;
        BattleRadius.layer = 2;
        BattleRadius.AddComponent<BattleRadius>();
        var circleCollider = BattleRadius.AddComponent<CircleCollider2D>();
        circleCollider.radius = 4f;
    }
    public void OnPosition(EnemyEntity entity, EntitasVec3 value)
    {
        if(this!=null)
        transform.position = value;
    }
    private void OnDestroy()
    {
        
        enemyEntity = null;
    }


    public void DestoryView()
    {
        enemyEntity.RemoveBaseView();
        Destroy(gameObject);
    }

    public string GetName()
    {
        return name;
    }

    public string GetTag()
    {
        return tag;
    }

    public void OnSpriteFlip(EnemyEntity entity, bool FlipX, bool FlipY)
    {
        SpriteRender.flipX = FlipX;
        SpriteRender.flipY = FlipY;
    }

   private int SetDamage(string enemyname)
    {
        string basename = enemyname.Remove(enemyname.IndexOf(' '));
        switch (basename)
        {
            case "Hornet":
                return 10;

            default:
                return 0;
        }
    }
}

